package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.enumerate.DownloadResult;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 发送卡参数下载事件
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SenderFileDownloadFinish implements CompleteEntityEvent {

    /**
     * 发送设备序号 -1为整个屏幕
     */
    private Integer prcIndex;

    /**
     * 下载路径
     */
    private String url;

    /**
     * 下载结果上报
     */
    private DownloadResult downloadResult;

    @Override
    public SenderFileDownloadFinish byteConvertObject(ByteString bytes) {
        CompleteEvent.SenderFileDownloadFinish senderFileDownloadFinish = null;
        try {
            senderFileDownloadFinish = CompleteEvent.SenderFileDownloadFinish.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(senderFileDownloadFinish);
        this.setPrcIndex(senderFileDownloadFinish.getPrcIndex());
        this.setUrl(senderFileDownloadFinish.getUrl());
        CompleteEntity.DownloadResult downloadResult1 = senderFileDownloadFinish.getDownloadResult();
        this.setDownloadResult(DownloadResult.getDownloadResultStruct(downloadResult1));
        return this;
    }
}

